
const Constant = require("./constant.js");
const PageTag = require("./momoGhsPageTag.js");
// const PageUtil = require("./pageUtil.js");
// const RunDefine = require("./runDefine.js");



// 操作 登录
const doLogin = () => {
    // let textList = className("TextView").find();
    // console.log(Constant.UI_SDK_VERSION + "判断是否为登录页-textList ", textList.length)
    // for (let index = 0; index < textList.length; index++) {
    //     let tv = textList[index];
    //     let curText = tv.text() || ""
    // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)
    // }

    let editList = className("EditText").find();
    // console.log("判断是否为登录页-editList ", editList.length)
    console.log(Constant.UI_SDK_VERSION + "开始操作登录");
    for (let index = 0; index < editList.length; index++) {
        let tv = editList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: "+ index+ ", 内容: ", curText)

        if (curText.indexOf(PageTag.loginPageTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "设置登录手机号: ", Constant.getMtn());
            tv.setText(Constant.getMtn());

            sleep(1000);
            console.log(Constant.UI_SDK_VERSION + "点击登录");
            click("NEXT");
            sleep(500)
            click("NEXT");
            sleep(500)
            click("NEXT");

            editList.length = 0;
            editList = null;
            return true;
        }
    }

    editList.length = 0;
    editList = null;
    console.log(Constant.UI_SDK_VERSION + "操作登录结束: 当前不是登录页")
    return false;
}

// 操作 登录-pin
const doPin = () => {
    let editList = className("EditText").find();
    // console.log(Constant.UI_SDK_VERSION + "判断是否为登录页-editList ", editList.length)
    console.log(Constant.UI_SDK_VERSION + "开始操作登录-pin");
    if (editList.length > 0) {
        let tv = editList[0];
        console.log(Constant.UI_SDK_VERSION + "设置登录PIN: ", Constant.getPin());
        tv.setText(Constant.getPin());
        sleep(1000);
        click("SIGN IN");
        sleep(500)
        click("SIGN IN");

        editList.length = 0;
        editList = null;
        return true;
    }

    editList.length = 0;
    editList = null;
    console.log(Constant.UI_SDK_VERSION + "操作登录结束: 当前不是登录-pin页")
    return false;
}

module.exports = {
    doLogin: doLogin,
    doPin: doPin
};